/*
 * @author: huangww
 * @Date: 2021-02-24 14:43:28
 * @LastEditTime: 2021-02-26 10:16:10
 * @FilePath: \fmgr\src\core\net\HTTPApi.ts
 */
namespace core {
  /**
   * http 消息封装
   */
  export class HTTPApi {
    static send(url, param: Params, msg: Msg) {
      let request: egret.HttpRequest = new egret.HttpRequest();
      request.open(url, egret.URLRequestMethod.POST);
      request.responseType = egret.HttpResponseType.TEXT;
      request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
      request.addEventListener(egret.Event.COMPLETE, (event: egret.Event) => {
        let req = <egret.HttpRequest>event.currentTarget;
        let data = JSON.parse(req.response);
        if (data.success) {//保存成功
          let msgs = new Msg(msg.mode, msg.action, data);
          DataMgr.getInstance().setData(msgs);
        } else {
          //错误情况处理
        }
      }, this);
      request.addEventListener(egret.IOErrorEvent.IO_ERROR, () => {

      }, this);
      request.send(param.getParams());
    }
  }
}